//
//  WebViewController.m
//  PinkOK
//
//  Created by BenyLiang on 13-10-15.
//  Copyright (c) 2013年 PinkOK. All rights reserved.
//

#import "IMTWebViewController.h"
#import "NSObject+HUD.h"
#import "IMTConfig.h"
#import "KSLanguageManager.h"
#import "UIViewController+NavButton.h"

@interface IMTWebViewController ()<WKNavigationDelegate,WKUIDelegate>
@property (nonatomic , strong)WKWebView *webView;
@end

@implementation IMTWebViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    // Do any additional setup after loading the view.
    UIBarButtonItem *left = [[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"nav_back_white"] imageWithRenderingMode:(UIImageRenderingModeAlwaysOriginal)] style:(UIBarButtonItemStylePlain) target:self action:@selector(backButtonPressed)];
    self.navigationItem.leftBarButtonItem = left;

    [self.navigationController.navigationBar setTitleTextAttributes:
     
     @{NSFontAttributeName:[UIFont systemFontOfSize:19],
       NSForegroundColorAttributeName:[UIColor whiteColor]}];

    WKWebViewConfiguration *config = [[WKWebViewConfiguration alloc] init];
    config.selectionGranularity = WKSelectionGranularityDynamic;
    config.allowsInlineMediaPlayback = YES;
    WKPreferences *preferences = [WKPreferences new];
    //是否支持JavaScript
    preferences.javaScriptEnabled = YES;
    //不通过用户交互，是否可以打开窗口
    preferences.javaScriptCanOpenWindowsAutomatically = YES;
    config.preferences = preferences;
    WKWebView *webview = [[WKWebView alloc] initWithFrame:CGRectMake(0, 0, kIMTScreenWidth, kIMTScreenHeight) configuration:config];
    [self.view addSubview:webview];
    webview.navigationDelegate = self;
    webview.UIDelegate = self;
    self.webView = webview;
    self.webView.hidden = YES;
    [webview addObserver:self forKeyPath:@"title" options:NSKeyValueObservingOptionNew context:NULL];

    if (self.content) {
        [self.webView loadHTMLString:self.content baseURL:nil];
    } else {
        [self loadUrl];
    }
}
#pragma mark - KVO回馈
-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSKeyValueChangeKey,id> *)change context:(void *)context{
   if ([keyPath isEqualToString:@"title"]){
        self.title = change[@"new"];
    }
}

- (void)loadUrl {
    if (self.url) {
        NSURL *url = [[NSURL alloc] initWithString:self.url];
        NSURLRequest *request = [NSURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:60];
        [self.webView loadRequest:request];
    }
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [[UIApplication sharedApplication] setStatusBarHidden:NO];
    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleDefault];
    self.tabBarController.tabBar.hidden = YES;
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    [self dismissHUD];
}

- (void)dealloc {
    [self.webView removeObserver:self forKeyPath:@"title"];
    [self.webView setNavigationDelegate:nil];
    [self.webView setUIDelegate:nil];
}

#pragma mark - Action

- (void)backButtonPressed {
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark - WebViewDelegate
/* 页面开始加载 */
- (void)webView:(WKWebView *)webView didStartProvisionalNavigation:(WKNavigation *)navigation{
    [self showProgressHUDWithText:IMTNewCarLocalizedString(@"prompt_loading")];
}

- (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation{
    [self dismissHUD];
    self.webView.hidden = NO;
    [webView evaluateJavaScript:@"document.body.style.fontFamily = 'helvetica'" completionHandler:nil];
}

@end
